package gui;

import gui.browse.Browse;
import gui.browse.BrowseListener;
import gui.util.GUIUtils;
import gui.util.JFrameCadastro;

import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.nio.charset.CodingErrorAction;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import model.Manutencao;

import com.jgoodies.forms.layout.CellConstraints;

import controller.ComputadorController;
import controller.ManutencaoController;

public class ManutencaoGUI extends JFrameCadastro {
	private static final long serialVersionUID = 1L;

	public JPanel panelCentral;
	public JButton botaoBrowse;
	public JButton botaoComputadorBrowse;
	
	public JTextField editCodigo;
	public JTextField editComputador;
	public JTextField editComputadorID;
	public JTextField editCusto;
	public JTextArea editDefeito;
	public JTextArea editSolucao;
	
	private JFormattedTextField editData;
	private MaskFormatter maskData;

	public ActionListener actionIncluir;
	public ActionListener actionAlterar;
	public ActionListener actionExcluir;
	public ActionListener actionCancelar;
	public ActionListener actionGravar;
	
	public ManutencaoGUI() {
		super();
		init();
	}

	public void init(){
		this.setTitle("Cadastro de Manutenção");
	}

	public JPanel getPanelCentral() {
		if (panelCentral == null){
			panelCentral = new JPanel(GUIUtils.getLayout(9, 20));
			CellConstraints  cc = new CellConstraints();

			panelCentral.add(new JLabel("Codigo"),		cc.xywh(  2, 4,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditCodigo(),			cc.xywh( 10, 4,  5, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(getBotaoBrowse(),			cc.xywh( 16, 4,  2, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(new JLabel("Computador"),	cc.xywh(  2, 6,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditComputador(),		cc.xywh( 10, 6,  5, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
			panelCentral.add(getEditComputadorID(),		cc.xywh( 16, 6,  1, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
			panelCentral.add(getBotaoComputadorBrowse(),cc.xywh( 18, 6,  2, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
			
			panelCentral.add(new JLabel("Data"),		cc.xywh(  2, 8,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditData(),				cc.xywh( 10, 8,  8, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
			
			panelCentral.add(new JLabel("Custo"),		cc.xywh( 20, 8,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(getEditCusto(),			cc.xywh( 28, 8,  8, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));

			panelCentral.add(new JLabel("Defeito"),		cc.xywh(  2, 10,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(new JScrollPane(getEditDefeito(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), cc.xywh( 10, 10, 26, 3, CellConstraints.FILL, CellConstraints.DEFAULT ));
			
			panelCentral.add(new JLabel("Solu��o"),		cc.xywh(  2, 14,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCentral.add(new JScrollPane(getEditSolucao(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
					JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), cc.xywh( 10, 14, 26, 3, CellConstraints.FILL, CellConstraints.DEFAULT ));

		}
		return panelCentral;
	}

	public JTextField getEditCodigo() {
		if (editCodigo == null){
			editCodigo = new JTextField();
			editCodigo.setEditable(false);
		}
		return editCodigo;
	}
	
	public JButton getBotaoBrowse(){
		return super.getBotaoBrowse(ManutencaoController.getInstance());
	}
	
	public JTextField getEditComputador() {
		if (editComputador == null){
			editComputador = new JTextField();
			editComputador.setEditable(false);
			editComputador.setEnabled(false);
		}
		return editComputador;
	}
	
	public JTextField getEditComputadorID() {
		if (editComputadorID == null){
			editComputadorID = new JTextField();
			editComputadorID.setEditable(false);
			editComputadorID.setEnabled(false);
		}
		return editComputadorID;
	}
	
	public JButton getBotaoComputadorBrowse() {
		if (botaoComputadorBrowse == null){
			botaoComputadorBrowse = new JButton("...");
			botaoComputadorBrowse.setEnabled(false);
			botaoComputadorBrowse.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Browse frame = new Browse(ComputadorController.getInstance());
					frame.addBrowseListener(new BrowseListener() {
						
						@Override
						public void linhaClicada(HashMap<String, Object> dados) {
							getEditComputador().setText((String) dados.get("descricao"));
							getEditComputadorID().setText(String.valueOf(dados.get("codigo")));
							
						}
					});
					frame.getFocusableWindowState();
				}
			});
		}
		return botaoComputadorBrowse;
	}

	
	public JTextField getEditCusto() {
		if (editCusto == null){
			editCusto = new JTextField();
			editCusto.setEnabled(false);
		}
		return editCusto;
	}
	
	public JFormattedTextField getEditData() {
		if (editData == null){
			editData = new JFormattedTextField(getMaskData());
			editData.setEnabled(false);
		}
		return editData;
	}
	public MaskFormatter getMaskData() {
		if (maskData == null){
			try {
				maskData = new MaskFormatter("##/##/####");
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return maskData;
	}
	
	public JTextArea getEditDefeito() {
		if (editDefeito == null){
			editDefeito = new JTextArea();
			editDefeito.setEnabled(false);
		}
		return editDefeito;
	}
	public JTextArea getEditSolucao() {
		if (editSolucao == null){
			editSolucao = new JTextArea();
			editSolucao.setEnabled(false);
		}
		return editSolucao;
	}
	
	
	
	@Override
	public ActionListener getActionIncluir() {
		if (actionIncluir == null){
			actionIncluir = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					limparDados();
					escreverRegistro(true);
					getEditCodigo().setText("0");
				}
			};	
		}
		return actionIncluir;
	}

	@Override
	public ActionListener getActionAlterar() {
		if (actionAlterar == null){
			actionAlterar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					escreverRegistro(true);
				}
			};
		}
		return actionAlterar;
	}

	@Override
	public ActionListener getActionExcluir() {
		if (actionExcluir == null){
			actionExcluir = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getEditCodigo().getText() != null && !getEditCodigo().getText().trim().isEmpty()){
						try {
							int opcao = (JOptionPane.showConfirmDialog(null, "Tem certeza que deseja excluir o registro "+ getEditCodigo().getText() +"?",
									"T�tulo", JOptionPane.YES_NO_OPTION));
							if (opcao == JOptionPane.YES_OPTION) {
								ManutencaoController.getInstance().excluirManutencao(Integer.valueOf(getEditCodigo().getText().trim()));
								limparDados();
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
			};
		}
		return actionExcluir;
	}

	@Override
	public ActionListener getActionCancelar() {
		if (actionCancelar == null){
			actionCancelar = new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					limparDados();
					escreverRegistro(false);
				}
			};
		}
		return actionCancelar;
	}

	@Override
	public ActionListener getActionGravar() {
		if (actionGravar == null){
			actionGravar = new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (getEditCodigo().getText().trim() != null && !getEditCodigo().getText().trim().isEmpty()){
						if (getEditCodigo().getText().trim().equals("0")){
							try {
								String custo = getEditCusto().getText();
								custo = custo.replaceAll(",", ".");
								ManutencaoController.getInstance().inserirManutencao(
										Integer.valueOf(getEditComputador().getText().trim()),
										getEditDefeito().getText(), 
										getEditSolucao().getText(), 
										Double.valueOf(custo),
										getEditData().getText());
							} catch (Exception e) {
								e.printStackTrace();
							}
						} else {
							try {
								String custo = getEditCusto().getText();
								custo = custo.replaceAll(",", ".");
								ManutencaoController.getInstance().alterarManutencao(
										Integer.valueOf(getEditCodigo().getText().trim()), 
										Integer.valueOf(getEditComputador().getText().trim()),
										getEditDefeito().getText(), 
										getEditSolucao().getText(), 
										Double.valueOf(custo),
										getEditData().getText());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
					escreverRegistro(false);
					limparDados();
					getBotaoFechar().grabFocus();
				}
			};
		}
		return actionGravar;
	}

	public void limparDados(){
		getEditCodigo().setText("");
		getEditComputador().setText("");
		getEditDefeito().setText("");
		getEditSolucao().setText("");
		getEditCusto().setText("");
		getEditData().setText("");
	}

	public void escreverRegistro(boolean b){
		getBotaoCancelar().setEnabled(b);
		getBotaoGravar().setEnabled(b);
		getBotaoIncluir().setEnabled(!b);
		getBotaoAlterar().setEnabled(!b);
		getBotaoExcluir().setEnabled(!b);
		getBotaoFechar().setEnabled(!b);
		getEditCodigo().setEnabled(!b);
		getBotaoBrowse().setEnabled(!b);
		getEditComputador().setEnabled(b);
		getEditDefeito().setEnabled(b);
		getEditSolucao().setEnabled(b);
		getEditCusto().setEnabled(b);
		getEditData().setEnabled(b);
		getBotaoComputadorBrowse().setEnabled(b);
		getEditComputador().grabFocus();
	}

	@Override
	public void preencherCampos(HashMap<String, Object> dados) {
		Manutencao manu = null;
		try {
			manu = ManutencaoController.getInstance().buscarManutencaoPorId((Integer) dados.get("codigo"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		getEditCodigo().setText(String.valueOf(manu.getId()));
		getEditComputador().setText(manu.getComputador().getDescricao());
		getEditComputadorID().setText(String.valueOf(manu.getComputador().getId()));
		getEditData().setText(manu.getData());
		getEditCusto().setText(String.valueOf(manu.getCusto()));
		getEditDefeito().setText(manu.getDescricaoDefeito());
		getEditSolucao().setText(manu.getDescricaoSolucao());
		
		
	}
}
